<script type="text/javascript">
//disk_format
$(document).ready(function() {
    $.extend({
        ReloadHost: function() {
            $("#tb_node_mng").trigger('reloadGrid');
        }
    });

    if (window.interval_node_list) {} else {
        window.interval_node_list = window.setInterval("$.ReloadHost()", 60000);
    }

    $('.grid-search-down-arrow').on('click', function() {
        $(this).hide();
        $('.grid-search').slideDown('slow', function() {
            $('.grid-search-up-arrow').show();
        });
    });

    $('.grid-search-up-arrow').on('click', function() {
        $(this).hide();
        $('.grid-search').slideUp('slow', function() {
            $('.grid-search-down-arrow').show();
        });
    });

    function fusionnas_host_status(grid_obj, id) {
        var mds_running = $(grid_obj).getCell(id, "mds_running");
        var mds_total = $(grid_obj).getCell(id, "mds_total");
        var cds_running = $(grid_obj).getCell(id, "cds_running");
        var cds_total = $(grid_obj).getCell(id, "cds_total");
        var status_show = '';
        status_show = '<p>mds:' + mds_running + '/' + mds_total + '</p>' + '<p>cds:' + cds_running + '/' + cds_total + '</p>'
        return status_show
    }

    function lichd_status_formatter(cellvalue, option, row) {
        row = row2object(row, option.gid);
        var rowid = row[0];
        if (is_fusionnas) {
            var html = '';
            var mds_running = row.mds_running;
            var mds_total = row.mds_total;
            var cds_running = row.cds_running;
            var cds_total = row.cds_total;
            if (mds_running == mds_total) {
                html += '<p style="color:green;">mds:' + mds_running + '/' + mds_total + '</p>';
            } else if (mds_running == 0) {
                html += '<p style="color:red;">mds:' + mds_running + '/' + mds_total + '</p>';
            } else if (mds_running < mds_total) {
                html += '<p style="color:orange;">mds:' + mds_running + '/' + mds_total + '</p>';
            }
            if (cds_running == cds_total) {
                html += '<p style="color:green;">cds:' + cds_running + '/' + cds_total + '</p>';
            } else if (cds_running == 0) {
                html += '<p style="color:red;">cds:' + cds_running + '/' + cds_total + '</p>';
            } else if (cds_running < cds_total) {
                html += '<p style="color:orange;">cds:' + cds_running + '/' + cds_total + '</p>';
            }
            return html;
        } else {
            return pack_status(cellvalue);
        }
    }
    function pack_status(cellvalue){
            var html = '';
            switch (cellvalue) {
                case 'running':
                    html += '<button type="button" class="btn btn-xs btn-success">' + cellvalue + '</button>';
                    break;
                case 'joining': case 'stopping':
                    html += '<button type="button" class="btn btn-xs btn-warning">' + cellvalue + '</button>';
                    break;
                case 'stopped': case 'shutdown':
                    html += '<button type="button" class="btn btn-xs btn-danger">' + cellvalue + '</button>';
                    break;
                case 'deleting':
                    html += '<button type="button" class="btn btn-xs btn-warning">' + cellvalue + '</button>';
                    break;
                default:
                    html += '';
                    break;
            }
            return html
    }

    var initial_width;
    $('#tb_node_mng').jqGrid({
        url: '/host/grid',
        //postData: postData,
        datatype: 'json',
        mtype: 'GET',
        styleUI: 'Bootstrap',
        colNames: ["编号", '[[ "label hostname"|translate ]]', 'IP', '状态','[[ "label service"|translate ]]', '[["label Protection Domain"|translate]]',
            '[[ "label disk number"|translate ]]', '是否在线', 'core', '[[ "label usage"|translate ]]', '内存总量(M)',
            '[[ "label memory usage"|translate ]]', '存储总量(G)', '[[ "label storage"|translate ]]', 'swap_total',
            'lichd_stat',
            {% if is_fusionnas %}
            'mds_total', 'mds_running', 'cds_total', 'cds_running'
            {% else %}
            'warntip', 'errortip', '操作'
            {% endif%}
        ],
        colModel: [{
            name: 'id',
            index: 'id',
            width: 20,
            hidden: true
        }, {
            name: 'name',
            index: 'name',
            width: 30
        }, {
            name: 'ip',
            index: 'ip',
            width: 35,
            formatter: function(cellvalue, option, row) {
                return '<a href="#" onclick="detail_open(\'' + option.gid + '\',\'' + option.rowId + '\')">' + cellvalue + '</a>';
            },
            unformat: function(cellvalue) {
                return cellvalue;
            }
        }, {
            name: 'status',
            index: 'status',
            width: 30,
            sortable: false,
            // hidden: !is_fusionnas,
            formatter: function(cellvalue, option, row) {
                return pack_status(cellvalue)
            },
            unformat: function(cellvalue) {
                return cellvalue;
            }
        }, {
            name: 'lichd_status',
            index: 'lichd_status',
            width: 35,
            formatter: function(cellvalue, option, row) {
                return lichd_status_formatter(cellvalue, option, row);
            },
            unformat: function(cellvalue) {
                return cellvalue;
            }
        }, {
            name: 'protection_domain',
            index: 'protection_domain',
            width: 25,
        	hidden: is_fusionnas, 
        }, {
            name: 'disk_number',
            index: 'disk_number',
            width: 25,
            hidden: true
        }, {
            name: 'is_join_show',
            index: 'is_join_show',
            width: 30,
            hidden: true
        }, {
            name: 'cpu_cores',
            index: 'cpu_cores_num',
            width: 20
        }, {
            name: 'usage_cpu',
            index: 'cpu_util',
            width: 35
        }, {
            name: 'mem_total',
            index: 'mem_str',
            width: 25,
            hidden: true
        }, {
            name: 'usage_mem',
            index: 'usage_mem',
            width: 40,
            sortable: false,
            formatter: function(cellvalue, option, row) {
                var percent = cellvalue.split('%')[0];
                return '<div style="margin-top:12px;height:12px;width:70%;float:left;"><div class="progressBar">' +
                    '<div class="progress_bar" style="width: '+ cellvalue +'"></div>' +
                    '</div></div>' +
                    '<div style="margin-top:11px;font-size:8px;line-height:10px;height:10px;width:30%;float:left;text-align:center;">' + cellvalue + '</div>';
            },
            unformat: function(cellvalue) {
                return cellvalue;
            }
        }, {
            name: 'disk_total',
            index: 'disk_total',
            width: 25,
        }, {
            name: 'usage_disk',
            index: 'usage_disk',
            width: 35,
            sortable: false,
            hidden: true
        }, {
            name: 'swap_total',
            index: 'swap_total',
            width: 20,
            hidden: true
        }, {
            name: 'lichd_status_hide',
            index: 'lichd_status_hide',
            width: 40,
            hidden: true
        },
        {% if is_fusionnas %}
        {
            name: 'mds_total',
            index: 'mds_total',
            width: 40,
            hidden: true
        },{
            name: 'mds_running',
            index: 'mds_running',
            width: 40,
            hidden: true
        },{
            name: 'cds_total',
            index: 'cds_total',
            width: 40,
            hidden: true
        },{
            name: 'cds_running',
            index: 'cds_running',
            width: 40,
            hidden: true
        }

        {% else %}
        {
            name: 'warntip',
            index: 'warntip',
            width: 40,
            hidden: true
        }, {
            name: 'errortip',
            index: 'errotip',
            width: 40,
            hidden: true
        }, {
            name: 'opearte',
            index: 'operate',
            width: 40,
            detailHidden: true,
            formatter: function(cell, option, row) {
                row = row2object(row, option.gid);
                var html = '';
                switch (row['lichd_status']) {
                    case 'running':
                        html += '<button title="停止" class="btn btn-blue" onclick=service_stop("' + row['id'] + '","' + row['name'] + '")>停止</button><button title="删除" style="margin-left:10px;" class="btn btn-blue" onclick=confirm_host_delete("' + row['id'] + '","' + row['name'] + '")>删除</button>'
                        break;
                    case 'stopped':
                        html += '<button title="启动" class="btn btn-blue" onclick=host_start("' + row['id'] + '")>启动</button><button title="删除" style="margin-left:10px;" class="btn btn-blue" onclick=confirm_host_delete("' + row['id'] + '","' + row['name'] + '")>删除</button>'
                        break;
                    case 'joinning': case 'stopping':
                        html += '<button title="删除" class="btn btn-blue" onclick=confirm_host_delete("' + row['id'] + '","' + row['name'] + '")>删除</button>'
                        break;
                }
                return html;
			},
        }
        {% endif %}
        ],
        page: 1,
        rowNum: 10,
        sortname: 'id',
        sortorder: 'desc',
        altclass: 'ui-priority-secondary',
        altRows: true,
        autowidth: true,
        multiselect: false,
        multiboxonly:true,
        multiselectWidth:'60px',
        //width: 1200,
        //height: mainpanelHeight,
        beforeSelectRow:function(rowid, e) {
            if($(e.target).is('input')) {
                return true;
            }
            return false;
        },
        gridComplete: function() {
            var gridData = $(this).jqGrid('getRowData');
            jqgrid_page($(this), gridData);
            jqgrid_resize($(this));
        }
    });

    $("#host_add").click(function() {
        var host_val = $('#tb_node_mng').jqGrid('getRowData');
        if(host_val.length > 0 || is_fusionnas){
            angular.element('#host_add').scope().open("/host/add");
        } else {
            angular.element('#host_add').scope().open("/host/config");
        }
        //angular.element('#host_add').scope().open("/host/add");
    });
    $("#cluster_import").click(function() {
        $('#div_cluster_import').dialog('open');
        $('#div_cluster_import').load('/host/import');
    });
    $('#getall').click(function() {
        var ids=$('#tb_node_mng').jqGrid('getGridParam','selarrrow');

    });

});

function service_stop(host_id, ip) {
    SelfConfirm("确认停止服务" + ip + "?", host_stop, host_id, "停止服务提示");
}

function confirm_host_delete(host_id, ip) {
    msg = "确认删除节点" + ip + "?";
    SelfConfirm(msg, host_delete, host_id, "删除提示");
}

function host_for_cluster(name, id) {
    $("#tb_node_mng").jqGrid('setGridParam', {
        page: 1,
        postData: {
            'cluster_id': id
        },
        url: '/grid_host'
    }).trigger('reloadGrid');
}

function host_delete(host_id) {
    $.ajax({
        url: "/host/delete",
        type: "POST",
        dataType: "json",
        data: {
            'host_id': host_id
        },
        success: function(data) {
            if (data.reply.is_success) {
                $("#tb_node_mng").trigger('reloadGrid');
            } else {
                error = data.reply.error;
                console.log(data)
                SelfAlert(error);
            }
        }
    });
}

function host_stop(host_id) {
    $.ajax({
        url: "/host/stop",
        type: "POST",
        dataType: "json",
        data: {
            'host_id': host_id
        },
        success: function(data) {
            if (data.reply.is_success) {
                $("#tb_node_mng").trigger('reloadGrid');
            } else {
                error = data.reply.error;
                SelfAlert(error);
            }
        }
    });
}

function host_start(host_id) {
    $.ajax({
        url: "/host/start",
        type: "POST",
        dataType: "json",
        data: {
            'host_id': host_id
        },
        success: function(data) {
            if (data.reply.is_success) {
                $("#tb_node_mng").trigger('reloadGrid');
            } else {
                error = data.reply.error;
                SelfAlert(error);
            }
        }
    });
}
</script>
<div class="panel panel-f-style">
    <div class="panel-body">
        <div class="title-tag">
            <h3>节点</h3>
            <p>节点管理</p>
        </div>
        <div class="grid-search" style="display: none;">
            <div class="node-select">
                <span>选择节点：</span>
                <select class="f-select">
                    <option>select1</option>
                    <option>select1</option>
                    <option>select1</option>
                </select>
                <span class="path">选择路径：</span>
                <select class="f-select">
                    <option>select1</option>
                    <option>select1</option>
                    <option>select1</option>
                </select>
            </div>
            <div class="time-select">
                <span>选择时间：</span>
                <input type="text" />
                <span class="split-line">----</span>
                <input type="text" />
            </div>
            <div class="type">
                <span>分类查看：</span>
                <span class="detail-type active">全部</span>
                <span class="detail-type">iscsi</span>
                <span class="detail-type">NBD</span>
            </div>
        </div>
        <div class="grid-tools">
            <div class="grid-search-up-arrow" style="display: none;">
                <i class="fa fa-angle-up"></i>
            </div>
            <div class="grid-search-down-arrow"  style="display: none;">
                <i class="fa fa-angle-down"></i>
            </div>
            <div class="grid-operate pull-left">
                <button class="btn btn-blue" type="button" id="host_add" ng-controller="HostModalCtrl">添加节点</button>
                <!-- <button class="btn btn-blue" type="button"><i class="glyphicon glyphicon-stop"></i> 停止</button>
                <button class="btn btn-blue" type="button"><i class="glyphicon glyphicon-trash"></i>删除</button> -->
            </div>
        </div>
        <div class="grid-body" id="gridttt">
            <table id="tb_node_mng"></table>
        </div>
    </div>
</div>
